<?php

require_once(dirname(__FILE__).'/ApiPoolProtocol.php');

class ApiPoolResponse
{
	public function getResult() {
		return $this->result_;
	}
	public function setResult($result) {
		$this->result_ = $result;
		$this->result_decode_ = json_decode($result, true);
	}
	
	public function getError() {
		return $this->error_;
	}
	public function setError($error) {
		$this->error_ = $error;
	}
	
	public function getId() {
		return $this->id_;
	}
	public function setId($id) {
		$this->id_ = $id;
	}
	
	public function getErrorInfo() {
		return $this->error_info_;
	}
	public function setErrorInfo($info) {
		$this->error_info_ = $info;
	}

	public function getAppErrorCode() {
		if ($this->result_decode_ == null)
			return null;

		return $this->result_decode_[ApiPoolProtocol::getAppErrorCodeKey()];
	}
	public function setAppErrorCode($code) {
		if ($this->result_decode_ != null)
			$this->result_decode_[ApiPoolProtocol::getAppErrorCodeKey()] = $code;
	}

	public function getAppErrorInfo() {
		if ($this->result_decode_ == null)
			return null;
		
		return $this->result_decode_[ApiPoolProtocol::getAppErrorInfoKey()];
	}
	public function setAppErrorInfo($info) {
		if ($this->result_decode_ != null)
			$this->result_decode_[ApiPoolProtocol::getAppErrorInfoKey()] = $info;
	}
	
	public function getAppDebugInfo() {
		if ($this->result_decode_ == null)
			return null;
		
		return $this->result_decode_[ApiPoolProtocol::getAppDebugInfoKey()];
	}
	public function setAppDebugInfo($info) {
		if ($this->result_decode_ != null)
			$this->result_decode_[ApiPoolProtocol::getAppDebugInfoKey()] = $info;
	}
	
	public function getAppData() {
		if ($this->result_decode_ == null)
			return null;
		
		return $this->result_decode_[ApiPoolProtocol::getAppDataKey()];
	}
	public function setAppData($data) {
		if ($this->result_decode_ != null)
			$this->result_decode_[ApiPoolProtocol::getAppDataKey()] = $data;
	}
	
	public function setErrorAndErrorInfo($errcode, $additional_info = "") {
		$this->setError($errcode);
		$error_info = ApiPoolError::translateError($errcode);
		if (! empty($additional_info))
			$error_info = $error_info."#".strval($additional_info);
		$this->setErrorInfo($error_info);
	}
	
	public function parsePackage($package) {
		$json_encode = ApiPoolProtocol::parsePackage($package);
		if ($json_encode == null) {
			return false;
		}
		
		$json_array = json_decode($json_encode, true);
		if ($json_array == null) {
			return false;
		}
		
		$this->setResult($json_array[ApiPoolProtocol::getResultKey()]);
		$this->setError($json_array[ApiPoolProtocol::getErrorKey()]);
		$this->setId($json_array[ApiPoolProtocol::getIdKey()]);
		$this->setErrorInfo($json_array[ApiPoolProtocol::getErrorInfoKey()]);
		return true;
	}
	
	private $result_;
	private $error_;
	private $id_;
	private $error_info_;
	private $result_decode_;
}

?>